#ifndef TCP_TCPSERVER_H
#define TCP_TCPSERVER_H

#include "Acceptor.h"
#include "TcpConnection.h"
#include <map>
#include <memory>

class TcpServer{
public:
    TcpServer(const InetAddress & listenAddr, std::string name);
    ~TcpServer();

    void start();

    void setConnectionCallback(const ConnectionCallback & cb){
        connectionCallback_ = cb;
    }

    void setMessageCallback(const MessageCallback &messageCallback){
        messageCallback_ = messageCallback;
    }

    void setCloseCallback(const CloseCallback &closeCallback){
        closeCallback_ = closeCallback;
    }

private:
    void newConnection(int sockfd, const InetAddress & peerAddr);

    void removeConnection(const TcpConnectionPtr & conn);

    typedef std::map<std::string, TcpConnectionPtr> ConnectionMap;
    const std::string name_;
    std::unique_ptr<Acceptor> acceptor_;

    ConnectionCallback connectionCallback_;
    MessageCallback messageCallback_;
    CloseCallback closeCallback_;
    // 上面这几个都是连接对应的调用函数, 负责填充进连接用的

    ConnectionMap  connections_;
    bool started_;
    int nextConnId_;
    // 持有当前存货的TcpConnection 的shar_ptr指针,
};

#endif //TCP_TCPSERVER_H
